package com.haoshuang.zhouzhoubang.ui.activity

import android.content.Context
import android.os.Bundle
import androidx.fragment.app.Fragment
import com.haoshuang.zhouzhoubang.databinding.ActivityMemberManagerBinding
import com.haoshuang.zhouzhoubang.model.event.GetTeamMemberListEvent
import com.haoshuang.zhouzhoubang.ui.adapter.ViewPagerAdapter
import com.haoshuang.zhouzhoubang.ui.fragment.MemberGroupFragment
import com.haoshuang.zhouzhoubang.ui.fragment.MemberManagerFragment
import com.haoshuang.zhouzhoubang.ui.vm.MemberManagerViewModel
import com.jeremyliao.liveeventbus.LiveEventBus
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.utils.ktx.convertInt
import com.mufeng.libs.utils.ktx.getString
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.view.clickWithTrigger
import com.mufeng.libs.utils.view.isBold


/**
 *  成员管理
 */
class MemberManagerActivity :
    BaseVMActivity<MemberManagerViewModel, ActivityMemberManagerBinding>() {

    companion object {
        fun launch(context: Context) {
            context.startPage<MemberManagerActivity>()
        }
    }

    private val fragments = arrayListOf<Fragment>()
    private lateinit var memberManagerFragment: MemberManagerFragment
    private lateinit var memberGroupFragment: MemberGroupFragment

    private var group_status = 0
    private var user_id = ""



    override fun initView(savedInstanceState: Bundle?) {

        group_status = intent.getIntExtra("group_status", 0)
        user_id = intent.getString("user_id")

        binding.titleBar.title = "门店成员"
        binding.titleBar.leftImageView().click { finish() }
        binding.titleBar.rightTextView().clickWithTrigger {
            // 分组管理
            startPage<GroupManagerActivity>("team_id" to team_id)
        }
        memberManagerFragment = MemberManagerFragment.getInstance(team_id.convertInt(), group_status, user_id)
        memberGroupFragment = MemberGroupFragment.getInstance()
        fragments.add(memberManagerFragment)
        fragments.add(memberGroupFragment)

        val adapter = ViewPagerAdapter(supportFragmentManager, lifecycle, fragments)
        binding.viewPager.adapter = adapter
        binding.viewPager.isUserInputEnabled = false

        selectTab(0)

        binding.tvMember.click { selectTab(0) }
        binding.tvGroup.click { selectTab(1) }

        binding.tvInvite.clickWithTrigger {
            // 邀请成员
            startPage<InvitingMembersActivity>("team_id" to team_id.toString())
        }

        binding.tvManager.clickWithTrigger {
            // 管理成员
            startPage<ManagerMemberActivity>(
                "group_status" to group_status,
                "user_id" to user_id
            )
        }

        binding.tvSeach2.clickWithTrigger {
            // 搜索
            val content = binding.tvSeach.text.toString()
            memberManagerFragment.search(content)
        }

    }

    private fun selectTab(index: Int) {
        binding.tvMember.isSelected = index == 0
        binding.tvGroup.isSelected = index != 0
        binding.tvMember.isBold = index == 0
        binding.tvGroup.isBold = index != 0
        binding.viewPager.currentItem = index
    }

    override fun initData() {

    }

    override fun startObserve() {

        LiveEventBus.get(GetTeamMemberListEvent::class.java).observe(this){
            binding.tvNum.text = "我的队员(${it.num})"
        }

    }

}